import { request } from "../../request/index"
import {showToast} from '../../utils/showmodel'

// pages/goods_detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsDetailList: {},
    iscollect:false
  },
  //当前商品信息
  GoodsDetailList: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // const {goods_id} = options
    // this.getGoodsDetail(goods_id)
  },
  //在onShow中获取传值过来的goods_id
  onShow(options) {
    //拿到页面栈数组 拿到goods_id渲染数据
    const pages = getCurrentPages();
    const { goods_id } = pages[pages.length - 1].options
    this.getGoodsDetail(goods_id)

  },


  //获取商品的详情数据
  async getGoodsDetail(goods_id) {
    const { data: res } = await request({
      url: '/goods/detail',
      data: { goods_id }
    })
    this.GoodsDetailList = res.message

    // 获取缓存中的商品收藏数组
    let collect = wx.getStorageSync('collect') || [];
    //判断当前商品是否已经被收藏   
    // some 有一个相同就返回true   得保证GoodsDetailList已经被拿到
    const iscollect =collect.some(v => v.goods_id=== this.GoodsDetailList.goods_id)
    this.setData({
      goodsDetailList: {
        goods_name: res.message.goods_name,
        goods_price: res.message.goods_price,
        //iphone手机不识别webp图片格式  最好找后台人员修改
        //临时自己改成jpg
        goods_introduce: res.message.goods_introduce.replace(/\.webp/g, '.jpg'),
        pics: res.message.pics
      },
      iscollect
    })

  },
  //预览图放大
  swiperClick(e) {
    //构建需要预览图片数组
    const Urls = this.GoodsDetailList.pics.map(item => item.pics_mid)
    //接收传递过来的index图片链接
    const current = e.currentTarget.dataset.url
    wx.previewImage({
      current,
      urls: Urls,
    });
  },

  //点击加入购物车
  handelCartAdd() {
    //获取缓存中的购物车数组
    let cart = wx.getStorageSync("cart") || [];
    //判断商品对象是否存在于购物车数组中
    let index = cart.findIndex(v => v.goods_id === this.GoodsDetailList.goods_id)
    if (index === -1) {
      //返回-1 为未找到第一次添加
      this.GoodsDetailList.num = 1
      this.GoodsDetailList.checked = true //是否勾选
      cart.push(this.GoodsDetailList)
    } else {
      //已经存在了 找到对应num++
      cart[index].num++
    }
    //将购物车重新缓存回去
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: '添加购物车成功',
      icon: 'success',
      //mask是否开启防抖  1.5s后触发
      mask: true,
    });
  },

  //点击购买
  handelCartBuy() {
    wx.showToast({
      title: '请到购物车结账',
      icon: 'error',
      mask: true,
    });
  },
  //  收藏按钮的点击
  async collectClick() {
    //判断当前商品是否存在于收藏中 ,如果有则删除该商品
    // 如果没有 获取收藏商品的数组 将这个收藏的商品添加进去 再缓存回去
    let iscollect = false
    let collect = wx.getStorageSync('collect')||[];
    let index = collect.findIndex(v=> v.goods_id ===this.GoodsDetailList.goods_id)
    if(index !== -1){
      //删除数组中的该商品
     collect.splice(index,1)
     iscollect = false
      await showToast({title:"取消成功",icon:"success"})
    }else{
      collect.push(this.GoodsDetailList)
      iscollect = true
      await showToast({title:"添加成功",icon:"none"})
    }
    //重新将新数组缓存回去
    wx.setStorageSync('collect',collect);
    this.setData({
      iscollect
    })
  }
})